#include "AvH264.h"
#include <iostream>

int main(int argc, char **argv)
{
    AvH264 h264;
    AvH264EncConfig conf;
    conf.bit_rate = 320000;
    conf.width = 320;
    conf.height = 240;
    conf.gop_size = 250;
    conf.max_b_frames = 0;
    conf.frame_rate = 25;
    h264.open(conf);

    cv::Mat frame;
    //--- INITIALIZE VIDEOCAPTURE
    cv::VideoCapture cap;
    // open the default camera using default API
    // cap.open(0);
    // OR advance usage: select any API backend
    int deviceID = 0;             // 0 = open default camera
    int apiID = cv::CAP_ANY;      // 0 = autodetect default API
                                  // open selected camera using selected API
    cap.open(deviceID, apiID);
    // check if we succeeded
    if (!cap.isOpened())
    {
        std::cerr << "ERROR! Unable to open camera\n" << std::endl;
        return -1;
    }

    //--- GRAB AND WRITE LOOP
    std::cout << "Start grabbing" << std::endl
        << "Press any key to terminate" << std::endl;
    for (;;)
    {
        // wait for a new frame from camera and store it into 'frame'
        cap.read(frame);
        // check if we succeeded
        if (frame.empty()) {
            std::cerr << "ERROR! blank frame grabbed\n" << std::endl;
            break;
        }

        // do encode
        AVPacket *pkt = h264.encode(frame);
        // do pkt
        //send(pkt->data, pkt->size);
        //std::this_thread::sleep_for(std::chrono::milliseconds(40));
    }
    h264.close();
    return 0;
}
